<?PHP

require_once __DIR__.DIRECTORY_SEPARATOR.'config.php';
require_once TEMP.'access_token.php';

function checkSignature($echo = FALSE)
{
    $signature = $_GET["signature"];
    $timestamp = $_GET["timestamp"];
    $nonce = $_GET["nonce"];

    $token = TOKEN;

    // $tmpArr = sha1(implode(sort(array($token, $timestamp, $nonce), SORT_STRING)));
    $tmpArr = array($token, $timestamp, $nonce);
    sort($tmpArr, SORT_STRING);
    $tmpStr = implode($tmpArr);
    $tmpStr = sha1($tmpStr);

    if($tmpStr == $signature) {
        if ($echo)  //微信修改服务器配置验证回复
            echo $_GET['echostr'];
        return TRUE;
    }
    return FALSE;
}

function parseXML($data)
{
    $xml = simplexml_load_string($data);
    if (!$xml)
        return FALSE;

    $post = array();
    foreach ($xml as $key => $val) {
        $post[$key] = $val->__toString();
    }

    return $post;
}

function getPost($xml = FALSE)
{
    $data = file_get_contents('php://input');
    if (!$data)
        return FALSE;

    return $xml ? parseXML($data) : $data;
}

function kyo_log()
{
    $s = "METHOD: ".$_SERVER['REQUEST_METHOD']. PHP_EOL;
    foreach ($_GET as $key => $val) {
        $s .= $key.' = '.$val.PHP_EOL;
        // echo $key, " = ", $val, '<br />', PHP_EOL;
    }

    $s .= PHP_EOL.'====================================='.PHP_EOL;

    foreach ($_POST as $key => $val) {
        $s .= $key.' = '.$val.PHP_EOL;
        // echo $key, " = ", $val, '<br />', PHP_EOL;
    }

    $s .= PHP_EOL.'====================================='.PHP_EOL;

    $data = getPost();
    $s .= 'input len: '.strlen($data).PHP_EOL;
    $s .= 'input data :'.PHP_EOL;
    $s .= $data;


    $s .= PHP_EOL.'====================================='.PHP_EOL;

    foreach ($_SERVER as $key => $val) {
        $s .= $key.' = '.$val.PHP_EOL;
        // echo $key, " = ", $val, '<br />', PHP_EOL;
    }
    $time = date('YmdHis');
    file_put_contents(TEMP.$time.'.log', $s);

    return $data;
}

function kyo_curl($url, $data = null, $method = 'GET', $header = null, $response = false)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    if ($data !== null)
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    if ($header !== null)
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    // curl_setopt($ch, CURLOPT_HEADER, TRUE);
    // print_r($data);

    $s = curl_exec($ch);

    $info = curl_getinfo($ch);

    curl_close($ch);

    if ($info['http_code'] >= 200 && $info['http_code'] <= 299)
        return $response ? array('data' => $s, 'info' => $info) : $s;

    return $response ? $info : FALSE;
}

function refresh_access_token()
{
    $url = WXURL.'token?grant_type=client_credential';
    $url .= '&appid='.APPID.'&secret='.APPSECRET;

    $wx = json_decode(kyo_curl($url));
    if (isset($wx->access_token)) {
        $s = <<< 'EOF'
<?php

const ACCESS_TOKEN = '{TOKEN}';

EOF;
        $s = str_replace('{TOKEN}', $wx->access_token, $s);
        return file_put_contents(TEMP.'access_token.php', $s);
    }

    return FALSE;
}

function refresh_wx_iplist()
{
    $url = WXURL.'getcallbackip?access_token='.ACCESS_TOKEN;

    $wx = json_decode(kyo_curl($url));
    if (isset($wx->ip_list)) {
        return file_put_contents(TEMP.'iplist.php', implode("\n", $wx->ip_list));
    }

    return FALSE;
}
